Skip to content

fix(logger): keep caller exclude set and honour NOTSET in copy_config_to_registered_loggers - #8834

Open
bardrop wants to merge 2 commits into
aws-powertools:developfrom
bardrop-oss:fix/logger-copy-config-exclude
Open

bardrop wants to merge 2 commits into
aws-powertools:developfrom
bardrop-oss:fix/logger-copy-config-exclude

Conversation

@bardrop

@bardrop bardrop commented Nov 15, 2026

Copy link
Copy Markdown

Issue number: closes #8829

Summary

Changes

In aws_lambda_powertools/logging/utils.py, copy_config_to_registered_loggers:

  • builds a new exclude set ({*(exclude or ()), source_logger_name, PACKAGE_LOGGER}) instead of calling exclude.update(...) on the caller's set;
  • picks the level with log_level is None instead of truthiness, so log_level=0 (NOTSET) is used and no longer falls back to the source logger's level.

User experience

Before:

exclude = {"mine"}
utils.copy_config_to_registered_loggers(source_logger=logger, exclude=exclude)
print(exclude)  # {"mine", "aws_lambda_powertools", "x"}

and log_level=0 set the external loggers to the source logger's level.

After: exclude is still {"mine"}, and log_level=0 sets the external loggers to NOTSET. Calls that pass no log_level, or a non-zero one, behave as before.

Verification

I used an AI assistant (Claude) to help write this change. I ran the tests below myself.

New functional tests in tests/functional/logger/required_dependencies/test_logger_utils.py (public API only): test_copy_config_to_ext_loggers_keeps_caller_exclude_set, test_copy_config_to_ext_loggers_log_level_notset.

On develop (fix reverted, tests kept):

FAILED tests/functional/logger/required_dependencies/test_logger_utils.py::test_copy_config_to_ext_loggers_keeps_caller_exclude_set
FAILED tests/functional/logger/required_dependencies/test_logger_utils.py::test_copy_config_to_ext_loggers_log_level_notset
2 failed, 178 passed

With the fix:

$ uv run pytest -q tests/unit/logger tests/functional/logger
206 passed, 7 warnings

Full uv run pytest -q tests/unit tests/functional on this branch: 4055 passed, 73 skipped, 3 failed. The same 3 fail on develop without my change, and they come from test order, not this PR: once tests/functional/metrics/datadog is collected, ddtrace is imported and adds dd.* keys to log records, so test_append_context_keys_handles_empty_dict and test_log_dict_key_strip_nones see extra keys (both pass when tests/functional/logger runs alone). The third, test_datadog_write_to_log_with_env_variable, passes when run alone. On my machine some full runs also hung in the Redis idempotency tests on develop too, so I ran the logger tests separately as well.

ruff format --check and ruff check on the changed files: clean. mypy aws_lambda_powertools/logging/: "Success: no issues found in 17 source files".


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@bardrop
bardrop requested a review from a team as a code owner November 15, 2026 16:00
@bardrop
bardrop requested a review from svozza November 15, 2026 16:00
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Nov 15, 2026
@leandrodamascena leandrodamascena added this to the 3.42.0 milestone Nov 15, 2026

@Manishram-ai Manishram-ai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this. I tried a few log_level values on develop and on this branch, and 0 now gives NOTSET while the exclude set I pass in stays the same. The one other change I found is log_level="", which falls back to the source logger's level on develop and raises ValueError: Unknown level: '' here. With the two suggestions below, the logger tests pass (207 tests), and ruff and mypy are clean.

Comment thread aws_lambda_powertools/logging/utils.py Outdated
List of logger names to exclude, by default None
"""
level = log_level or source_logger.log_level
level = source_logger.log_level if log_level is None else log_level

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old log_level or source_logger.log_level treated an empty string like None. Something like log_level=os.getenv("LIBS_LOG_LEVEL", "") would start failing on cold start after an upgrade. This keeps that fallback and still honours 0.

Suggested change
level = source_logger.log_level if log_level is None else log_level
level = source_logger.log_level if log_level in (None, "") else log_level

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks. Applied in the latest commit: an empty string now falls back to the source logger level again, and 0 still gives NOTSET.


# THEN external logger uses NOTSET instead of the source logger level
assert powertools_logger.level == log_level.CRITICAL.value
assert logger.level == log_level.NOTSET.value

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A test for the empty string. It fails on this branch with ValueError: Unknown level: '' and passes with the change above.

Suggested change
assert logger.level == log_level.NOTSET.value
assert logger.level == log_level.NOTSET.value
def test_copy_config_to_ext_loggers_empty_log_level_uses_source_level(stdout, logger, log_level):
# GIVEN an external logger and Powertools for AWS Lambda (Python) logger initialized
logger = logger()
powertools_logger = Logger(service=service_name(), level=log_level.CRITICAL.value, stream=stdout)
# WHEN configuration copied with an empty log_level
utils.copy_config_to_registered_loggers(
source_logger=powertools_logger,
log_level="",
include={logger.name},
)
# THEN external logger falls back to the source logger level
assert logger.level == log_level.CRITICAL.value

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this test too. It fails without the change and passes with it.

@sonarqubecloud

Copy link
Copy Markdown

@Manishram-ai Manishram-ai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding both! An empty log_level falls back to the source logger's level again, 0 still gives NOTSET, and the logger tests pass for me on the new commit.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

logger size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: copy_config_to_registered_loggers mutates the caller's exclude set and ignores log_level=0

3 participants